<?php
/* SVN FILE: $Id: behaviorable.php 1839 2008-10-22 12:10:13Z francis $ */
class BehaviorableBehavior extends ModelBehavior {
	var $settings = array();
	var $defaults=array(
		'beforeFind' => false,
		'afterFind' => false,
		'beforeValidation' => false,
		'afterValidation' => false,
		'beforeSave' => false,
		'afterSave' => false);
/**
 * Enter description here...
 *
 * @param AppModel $model
 * @param unknown_type $config
 */
	function setup(&$model, $config = array()) {
		$config = array_merge($this->defaults, $config);
		$this->settings[$model->alias] = $config;
	}
	function beforeFind(&$model, $query) {
		$config = $this->settings[$model->alias];
		/*
		$db =& ConnectionManager::getDataSource($model->useDbConfig);
		if (empty($query['fields'])) {
			$query['fields']=$db->fields($model);
		}
		if (empty($query['recursive'])) {
			$query['recursive']=0;
		}
		$result = $query;

		if ($config['beforeFind'] && ($query['recursive']>=0)) {
			$queryBehavior = $query;
			$queryBehavior['recursive']=-1;
			$assocs=array('belongsTo');
			foreach ($assocs as $assoc) {
				foreach ($model->{$assoc} as $modelCh => $modelOptions) {
					$queryBehavior=$model->{$modelCh}->Behaviors->trigger('beforeFind', array($queryBehavior), array('break' => true, 'breakOn' => false, 'modParams' => true));
				}
			}
		}
		*/
		// if ($queryBehavior!=$query && is_array($query)) {
			// $query = $queryBehavior;
		//}
		return $query;
	}
	function afterFind(&$model, $results, $primary) {
		$config = $this->settings[$model->alias];

		if (is_array($config['afterFind'])) {
			foreach ($config['afterFind'] as $mdl) {
				if (isset($model->{$mdl})) {
					$results=$model->{$mdl}->Behaviors->trigger($model->{$mdl}, 'afterFind', array($results, $primary), array('modParams' => true));
				}
			}
		}
		return $results;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function beforeSave(&$model) {
		$config = $this->settings[$model->alias];
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function afterSave(&$model, $created) {
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function beforeDelete(&$model) {
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function afterDelete(&$model) {
		return true;
	}
/**
 * Callback
 *
 * @param AppModel $model
 */
	function onError(&$model, $error) {

	}
}
?>